package com.bw.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.area.mapper.TbHouseAreaMapper;
import com.bw.area.service.TbHouseAreaService;
import com.bw.po.TbHouseArea;
import com.bw.result.ResponseState;
import com.bw.result.ResultVo;
import com.bw.vo.AreaVo;
import com.bw.vo.TbHouseAreaVo;
import com.bw.vo.TreeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 007
* @description 针对表【tb_house_area(地区表)】的数据库操作Service实现
* @createDate 2023-11-11 15:36:44
*/
@Service
public class TbHouseAreaServiceImpl extends ServiceImpl<TbHouseAreaMapper, TbHouseArea>
    implements TbHouseAreaService {

    @Override
    public ResultVo getOneAra() {
        TbHouseAreaVo tbHouseAreaVo = new TbHouseAreaVo();
        tbHouseAreaVo.setPid(0);
        QueryWrapper<TbHouseArea> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbHouseArea::getPid,tbHouseAreaVo.getPid());
        List<TbHouseArea> list = list(wrapper);
        ArrayList<TbHouseAreaVo> areaList = new ArrayList<>();
        for (TbHouseArea tbHouseArea : list) {
            TbHouseAreaVo houseAreaVo = new TbHouseAreaVo();
            BeanUtils.copyProperties(tbHouseArea,houseAreaVo);
            areaList.add(houseAreaVo);
        }
        return ResultVo.SUCCESS(areaList);
    }

    @Override
    public ResultVo getTwoArea(TbHouseAreaVo tbHouseAreaVo) {
        QueryWrapper<TbHouseArea> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbHouseArea::getPid,tbHouseAreaVo.getId());
        List<TbHouseArea> list = list(wrapper);
        ArrayList<TbHouseAreaVo> areaList = new ArrayList<>();
        for (TbHouseArea tbHouseArea : list) {
            TbHouseAreaVo houseAreaVo = new TbHouseAreaVo();
            BeanUtils.copyProperties(tbHouseArea,houseAreaVo);
            areaList.add(houseAreaVo);
        }
        return ResultVo.SUCCESS(areaList);
    }

    @Override
    public ResultVo getAreaTree() {
        //查询所有数据
        List<TbHouseArea> list = list();

        //查询一级分类
        List<TreeVo> collect = list.stream().filter(item -> {
            //判断
            if (item.getPid().equals(0)) {
                return true;
            } else {
                return false;
            }
        }).map(item -> {
            TreeVo treeVo = new TreeVo();
            treeVo.setValue(item.getId() + "");
            treeVo.setLabel(item.getName());
            return treeVo;
        }).collect(Collectors.toList());

        //便利分级查询
        collect.forEach(item->{
            subTree(list,item);
        });

        return ResultVo.SUCCESS(collect);
    }

    @Override
    public String getAreaById(Integer id) {
        QueryWrapper<TbHouseArea> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbHouseArea::getId,id);
        TbHouseArea houseArea = getOne(wrapper);
        return houseArea.getName();
    }

    @Override
    public AreaVo getAreas(Integer provinceId, Integer cityId, Integer countyId) {
        String provinceName = getAreaName(provinceId);
        String cityName = getAreaName(cityId);
        String countyName = getAreaName(countyId);
        AreaVo areaVo = new AreaVo();
        areaVo.setProvince(provinceName);
        areaVo.setCity(cityName);
        areaVo.setCounty(countyName);
        return areaVo;
    }

    private String getAreaName(Integer provinceId) {
        QueryWrapper<TbHouseArea> tbHouseAreaQueryWrapper = new QueryWrapper<>();
        tbHouseAreaQueryWrapper.lambda().eq(TbHouseArea::getId,provinceId);
        TbHouseArea tbHouseArea = getOne(tbHouseAreaQueryWrapper);
        return tbHouseArea.getName();
    }

    private  void  subTree(List<TbHouseArea> list,TreeVo treeVo){
        Integer id = Integer.parseInt(treeVo.getValue());

        List<TreeVo> collect = list.stream().filter(item -> {
            if (item.getPid().equals(id)) {
                return true;
            } else {
                return false;
            }
        }).map(item -> {
            TreeVo tree = new TreeVo();
            tree.setValue(item.getId() + "");
            tree.setLabel(item.getName());

            subTree(list, tree);
            return tree;
        }).collect(Collectors.toList());

        if (collect.size()>0){
            treeVo.setChildren(collect);
        }
    }
}




